package com.bena.udvtui2;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Window;
import android.view.WindowManager;


public class UDVirtualTourUI extends Activity {
	ImageButton startButton;
	ImageButton quitButton;
	String actionName;
	Intent intent;
	static final int INFO_PAGE_RESULT = 0;
	static final int MAP_PAGE_RESULT = 1;
	int seenLocs=0;
	String[] cs;// = new String[100];
	String temp;
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent){
		// This is for when an activity completes.  Requires startAcitivtyForResult(string,int);
		//Toast.makeText(UDVirtualTourUI.this, "reqC: "+requestCode+" resC: "+resultCode, Toast.LENGTH_SHORT).show();
		if(requestCode==UDVirtualTourUI.INFO_PAGE_RESULT && resultCode==-1){ 
			Log.e("ActivityResult","loading");
			Bundle bundle2 = intent.getExtras();
			seenLocs = bundle2.getInt("count");
			cs = bundle2.getStringArray("seen");
			for(int i=0; i<seenLocs;i++){  // print everything in the array
				Log.d("MAIN_RETURN_SEEN",cs[i]);
			}
		}
		else if(requestCode==UDVirtualTourUI.MAP_PAGE_RESULT && resultCode==-1){
			Log.e("ActivityResult","loading");
			Bundle bundle2 = intent.getExtras();
			seenLocs = bundle2.getInt("count");
			cs = bundle2.getStringArray("seen");
			for(int i=0; i<seenLocs;i++){  // print everything in the array
				Log.d("MAIN_RETURN_SEEN",cs[i]);
			}
		}
		else{
			Log.e("ActivityResult","fail");
		}
	}
	
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
    	Bundle bundle = new Bundle();
    	bundle.putStringArray("seen", cs);
    	bundle.putInt("count", seenLocs);
    	savedInstanceState.putBundle("bundle", bundle);
    	super.onSaveInstanceState(savedInstanceState);
    }

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState){
		// Only works for orientation changes not activity
		//Toast.makeText(AndroidTest01.this, "Loading State", Toast.LENGTH_SHORT).show(); 
		Log.e("LOADinstanceSTATE","loading");
		super.onRestoreInstanceState(savedInstanceState);
		Bundle bundle = new Bundle();
		bundle = savedInstanceState.getBundle("bundle");
		cs = bundle.getStringArray("seen");
		seenLocs = bundle.getInt("count");
	}

	
	/* Menu 
	 *   Get it working correct here then copy paste
	 *   onCreateOptionsMenu and onOptionsItemSelected 
	 *   to the other activities
	 */
	@Override    
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_resource, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.restart:
			 // prepare the alert box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertbox.setTitle("Reset?");
            // set the message to display
            alertbox.setMessage("Are you sure you would like to reset the tour?");
 
            // set a positive/yes button and create a listener
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    //Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
        			//Toast.makeText(UDVirtualTourUI.this, "The tour has reset.", Toast.LENGTH_SHORT).show(); 
        			for(int i=0; i<seenLocs; i++){
        				cs[i]=null;
        			}
        			seenLocs=0;
                }
            });
 
            // set a negative/no button and create a listener
            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
 
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    //Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                }
            });
 
            // display box
            alertbox.show();
			return true;
		case R.id.trabant:
			newActivityPrep("Trabant");
			return true;
		case R.id.perkins:
			newActivityPrep("Perkins");
			return true;
		case R.id.morris:
			newActivityPrep("Morris");
			return true;
		case R.id.evans:
			newActivityPrep("Evans");
			return true;
		case R.id.brown:
			newActivityPrep("Brown");
			return true;
		case R.id.memorial:
			newActivityPrep("Memorial");
			return true;
		case R.id.mitchell:
			newActivityPrep("Mitchell");
			return true;
		case R.id.gore:
			newActivityPrep("Gore");
			return true;
		case R.id.sharp:
			newActivityPrep("Sharp");
			return true;
		case R.id.dupont:
			newActivityPrep("Dupont");
			return true;
		case R.id.hullihen:
			newActivityPrep("Hullihen");
			return true;
		case R.id.wolf:
			newActivityPrep("Wolf");
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.main);
		cs = new String[100];
		startButton = (ImageButton) findViewById(R.id.imageStart);
		quitButton = (ImageButton) findViewById(R.id.homeQuit);

		quitButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Toast.makeText(UDVirtualTourUI.this, "Quitter...", Toast.LENGTH_SHORT).show();
				UDVirtualTourUI.super.onDestroy();
				UDVirtualTourUI.this.finish();
			}
		});
		startButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				actionName = "com.bena.udvtui2.showmap";  // set the next activity
				Bundle bundle = new Bundle();
        		bundle.putStringArray("seen",cs);
        		bundle.putInt("count", seenLocs);
        		Intent intent = new Intent(actionName);
        		intent.putExtra("bundle", bundle);
        		Log.d("MAIN: START","start activity");
				startActivityForResult(intent,MAP_PAGE_RESULT);
			}
		});

	}
	
	public void newActivityPrep(String location){
		Log.d("NEWACTPREP","Starting");
		actionName = "com.bena.udvtui2.infopage";  // the name of the next action
		intent = new Intent(actionName);  
		int seen=0;  // has the location been seen?  1: yes 0: no
		Bundle bundle = new Bundle();
		Log.d("NEWACTPREP","Starting while");
		for(int i=0; i<seenLocs;i++){  // scan the array for the new location
			Log.d("NEWACTPREP","while");
			if(cs[i].equals(location)){
				seen=1;   // location is already in the array
				temp=cs[i];
				cs[i]=cs[seenLocs-1];
				cs[seenLocs-1]=temp;
				break;
			}
		}
		Log.d("NEWACTPREP","while complete");
		if(seen==0){  // if it hasn't been seen, add it
			Log.d("NEWACTPREP","not seen");
			cs[seenLocs]=location;
			seenLocs++;
		}
		Log.d("NEWACTPREP","loading bundle");  // load into the bundle
		bundle.putStringArray("seen", cs);
		bundle.putInt("count", seenLocs);
		intent.putExtra("bundle", bundle);    // load into the intent
		Log.d("NEWACTPREP","startActivityForResult");
		startActivityForResult(intent,INFO_PAGE_RESULT);  // start the activity
	}
}
